package com.bcs.moon.test;

import com.bcs.compiler.TargetAssemblerX86;
import com.bcs.compiler.TargetSourceC;
import com.bcs.moon.XCompile;

public class UnitUtil {
	static XCompile runCompileScript(String script) {
		XCompile compiler = new TargetSourceC();
		compiler.initialize();
		compiler.parseScript("script", script);
		if (compiler.hasError)
			throw new RuntimeException("error in the script");
		compiler.compile("test.m");
		if (compiler.hasError)
			throw new RuntimeException("error in the script");
		return compiler;
	}

	static XCompile runCompileScriptX86(String script) {
		TargetAssemblerX86 compiler = new TargetAssemblerX86();
		compiler.initialize();
		compiler.parseScript("script", script);
		if (compiler.hasError)
			throw new RuntimeException("error in the script");
		compiler.compile("test.m");
		if (compiler.hasError)
			throw new RuntimeException("error in the script");
		
		compiler.getLinkMap();
		
		return compiler;
	}

	static XCompile runCompileScriptFail(String script) {
		XCompile compiler = new TargetSourceC();
		compiler.initialize();
		compiler.parseScript("script", script);
		if (compiler.hasError)
			return compiler;
		compiler.compile("test.m");
		if (compiler.hasError)
			return compiler;
		throw new RuntimeException("script should have failed");
	}

	static XCompile runCompileScripts(String... scripts) {
		XCompile compiler = new TargetSourceC();
		compiler.initialize();
		for (int idx = 0; idx < scripts.length; idx++)
			compiler.parseScript("script", scripts[idx]);
		compiler.compile("test.m");
		return compiler;
	}
}
